package com.bsb.biz.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.config.PayType;
import com.bsb.base.common.util.DateUtils;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbCardBuy;
import com.bsb.base.model.BsbCardBuyExample;
import com.bsb.base.model.BsbCardBuyExample.Criteria;
import com.bsb.base.model.BsbCardBuyUserFirm;
import com.bsb.base.model.BsbFirmExample;
import com.bsb.base.model.BsbUserExample;
import com.bsb.base.model.SysUser;
import com.bsb.biz.service.BsbCardBuyService;
import com.bsb.biz.shiro.UserUtils;




@Controller
public class BsbCardBuyController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbCardBuy/list.do";
	
	@Resource
	private BsbCardBuyService bsbCardBuyService;
	@RequiresPermissions("bsbCardBuy:list")
	@RequestMapping(value="/bsbCardBuy/list",method=RequestMethod.GET)
	public String list(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		
		String cardbuyId = (String) params.get("cardbuyId");
		String name = (String) params.get("name");
		String firm = (String) params.get("firm");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		
		BsbCardBuyExample bsbCardBuyExample = new BsbCardBuyExample();
		Criteria createCriteria = bsbCardBuyExample.createCriteria();
		SysUser currentUser = UserUtils.currentUser();
		Long firmId = currentUser.getFirmId();
		if(firmId==null){
			model.addAttribute("msg", "账号异常请联系管理员！");
			firmId=10000l;
			return "/BsbCardBuy/list";
			
		}else if(firmId!=10000){
			createCriteria.andFirmIdEqualTo(firmId.intValue());
		}
		
		createCriteria.andIsPayEqualTo(PayType.PAY_PAID.getCode());//已支付
		if (StringUtils.isNotBlank(cardbuyId)) {
			createCriteria.andUuidLike("%"+cardbuyId+"%");
		}
		if(startDate!=null){
			createCriteria.andPaymentDateGreaterThanOrEqualTo(startDate);
		}
		if(endDate!=null){
			endDate=DateUtils.addDays(endDate, 1);
			createCriteria.andPaymentDateLessThanOrEqualTo(endDate);
		}
		createCriteria.andDelFlagEqualTo(Global.SHOW);
		BsbUserExample userExample=null;
		if(StringUtils.isNotBlank(name)){
			userExample=new BsbUserExample();
			com.bsb.base.model.BsbUserExample.Criteria createCriteria2 = userExample.createCriteria();
			createCriteria2.andNameLike("%"+name+"%");
		}
		BsbFirmExample firmExample=null;
		if(StringUtils.isNotBlank(firm)){
			firmExample=new BsbFirmExample();
			com.bsb.base.model.BsbFirmExample.Criteria createCriteria2 = firmExample.createCriteria();
			createCriteria2.andNameLike("%"+firm+"%");
		}
		int totalCount =bsbCardBuyService.getCountCradBuyUserFirm(bsbCardBuyExample, userExample, firmExample);
		//int totalCount = bsbCardBuyService.countBsbCardBuy(bsbCardBuyExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum >0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
			if(maxPageNum==0){
				pageNum=1;
			}
		}
		bsbCardBuyExample.setPageIndex((pageNum - 1) * pageSize);
		bsbCardBuyExample.setPageSize(pageSize);
		bsbCardBuyExample.setOrderByClause("bsb_card_buy.paymentDate desc");
		//List<BsbCardBuy> bsbCardBuys = bsbCardBuyService.getPageBsbCardBuy(bsbCardBuyExample);
		List<BsbCardBuyUserFirm> buyUserFirms = bsbCardBuyService.getCardBuyUserFirms(bsbCardBuyExample, userExample, firmExample);
		
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);

		model.addAttribute("cardbuyId", cardbuyId);
		model.addAttribute("name", name);
		model.addAttribute("firm", firm);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("firmId", firmId);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbCardBuys", buyUserFirms);
		return "/BsbCardBuy/list";
	}
	
	@RequestMapping("/bsbCardBuy/toAdd")
	public String toAdd(Model model) {
		return "/BsbCardBuy/add";
	}

	@RequestMapping("/bsbCardBuy/add")
	public String add(Model model, @ModelAttribute("bsbCardBuy") BsbCardBuy bsbCardBuy) {
		bsbCardBuyService.addBsbCardBuy(bsbCardBuy);
		return redirect;
	}

	@RequestMapping("/bsbCardBuy/del")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		bsbCardBuyService.delBsbCardBuyByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/bsbCardBuy/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		BsbCardBuy bsbCardBuy = bsbCardBuyService.getBsbCardBuyByUuid(uuid);
		model.addAttribute("bsbCardBuy", bsbCardBuy);
		return "/BsbCardBuy/edit";
	}

	@RequestMapping("/bsbCardBuy/edit")
	public String edit(Model model, @ModelAttribute("bsbCardBuy") BsbCardBuy bsbCardBuy) {
		bsbCardBuyService.updateBsbCardBuy(bsbCardBuy);
		return redirect;
	}

	@RequestMapping("/bsbCardBuy/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		BsbCardBuy bsbCardBuy = bsbCardBuyService.getBsbCardBuyByUuid(uuid);
		model.addAttribute("bsbCardBuy", bsbCardBuy);
		return "/BsbCardBuy/view";
	}
}
